package cn.wsn2018.tools.ctrl;

import cn.wsn2018.tools.common.CommonValue;
import cn.wsn2018.tools.model.Log.ErrorLogInfo;
import cn.wsn2018.tools.model.Log.WarningLogInfo;
import cn.wsn2018.tools.model.MySessionContext;
import cn.wsn2018.tools.service.LogsInfoService;
import cn.wsn2018.tools.util.JsonUtil;
import cn.wsn2018.tools.util.LogUtil;
import cn.wsn2018.tools.util.RoleUtil;
import cn.wsn2018.tools.util.ServletUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author gmz.
 * @date 2018/12/14.
 * @time 20:50
 */
@RestController
@RequestMapping("/protocol")
public class LogsInfoController {
    private Logger logger = Logger.getLogger(LogsInfoController.class);

    @Autowired
    private LogsInfoService logsInfoService;

    /**
     * 分页查询，默认以createTime降序排列，查询所有日志信息
     * @param request
     * @param response
     * @throws Exception
     */
    /*@CrossOrigin
    @RequestMapping(value = "/logsInfo",method = RequestMethod.GET)
    public void findAllLogsSort(@RequestParam("pageNum")String pageNum,
                                @RequestParam ("pageSize") String pageSize,
                                @RequestParam("token") String token,
                                HttpServletRequest request, HttpServletResponse response) throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myRole = session.getAttribute("role").toString();
        if(CommonValue.ROLE_AUDITOR.equals(myRole)){
            String json = logsInfoService.findLogsByPageNum(pageNum,pageSize);
            ServletUtil.getInstance().responseOperating(response, json);
        }else {
            String json = JsonUtil.failedJson(CommonValue.JSON_ROLE_ERROR);
            ServletUtil.getInstance().responseOperating(response, json);
        }
    }*/

    @CrossOrigin
    @RequestMapping(value = "/logsInfo",method = RequestMethod.GET)
    public void findAllLogs(@RequestParam("username")String username,
                            @RequestParam("pageNum")String pageNum,
                            @RequestParam("pageSize")String pageSize,
                            @RequestParam("fromTime")String fromTime,
                            @RequestParam("toTime")String toTime,
                            @RequestParam("token") String token,
                            HttpServletRequest request, HttpServletResponse response) throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myRole = session.getAttribute("role").toString();
        if(CommonValue.ROLE_AUDITOR.equals(myRole)){
            String json = logsInfoService.findLogsByCond(username,pageNum,pageSize,fromTime,toTime);
            ServletUtil.getInstance().responseOperating(response, json);
        }else {
            String json = JsonUtil.failedJson(CommonValue.JSON_ROLE_ERROR);
            LogUtil.produceWarningLogs(username,
                    CommonValue.WARNING_TYPE_1,
                    CommonValue.WARNING_LEVEL_0,
                    CommonValue.LOGMODULE,
                    ServletUtil.getClientIp(request),
                    LogUtil.produceWarningMsg(CommonValue.WARNING_TYPE_1,username,RoleUtil.getRoleName(myRole),CommonValue.LOGMODULE)
            );
            ServletUtil.getInstance().responseOperating(response, json);
        }
    }

    /**
     * 分页查询，默认以createTime降序排列，查询某一操作员的日志信息
     * @param request
     * @param response
     * @throws Exception
     */
    /*@CrossOrigin
    @RequestMapping(value = "/logsInfo/username",method = RequestMethod.GET)
    public void findAllLogsSort(@RequestParam("username") String username,
                                @RequestParam("pageNum")String pageNum,
                                @RequestParam ("pageSize") String pageSize,
                                @RequestParam("token") String token,
                                HttpServletRequest request, HttpServletResponse response) throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myRole = session.getAttribute("role").toString();
        if(CommonValue.ROLE_AUDITOR.equals(myRole)){
            String json = logsInfoService.findLogsByPgNumAndName(username,pageNum,pageSize);
            ServletUtil.getInstance().responseOperating(response, json);
        }else {
            String json = JsonUtil.failedJson(CommonValue.JSON_ROLE_ERROR);
            ServletUtil.getInstance().responseOperating(response, json);
        }
    }

    *//**
     * 分页查询，查询时间范围内的所有用户的日志信息
     * @param request
     * @param response
     * @throws Exception
     *//*
    @CrossOrigin
    @RequestMapping(value = "/logsInfo/timeRange", method = RequestMethod.GET)
    public void findLogsByTimeRange(@RequestParam("fromTime")String fromTime,
                                    @RequestParam("toTime")String toTime,
                                    @RequestParam("pageNum")String pageNum,
                                    @RequestParam("pageSize")String pageSize,
                                    @RequestParam("token") String token,
                                    HttpServletRequest request,HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myRole = session.getAttribute("role").toString();
        if(CommonValue.ROLE_AUDITOR.equals(myRole)){
            String json = logsInfoService.findLogsByTime(pageNum,pageSize,fromTime,toTime);
            ServletUtil.getInstance().responseOperating(response, json);
        }else {
            String json = JsonUtil.failedJson(CommonValue.JSON_ROLE_ERROR);
            ServletUtil.getInstance().responseOperating(response, json);
        }

    }

    *//**
     * 分页查询，查询时间范围内、某一页、X用户的日志信息
     * 日志信息
     * @param request
     * @param response
     * @throws Exception
     *//*
    @CrossOrigin
    @RequestMapping(value = "/logsInfo/username/timeRange", method = RequestMethod.GET)
    public void findLogsByTimeRangeAndName(@RequestParam("username")String username,
                                           @RequestParam("pageNum")String pageNum,
                                           @RequestParam("pageSize")String pageSize,
                                           @RequestParam("fromTime")String fromTime,
                                           @RequestParam("toTime")String toTime,
                                           @RequestParam("token") String token,
                                            HttpServletRequest request,HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myRole = session.getAttribute("role").toString();
        if(CommonValue.ROLE_AUDITOR.equals(myRole)){
            String json = logsInfoService.findLogsByTimeAndName(username,pageNum,pageSize,fromTime,toTime);
            ServletUtil.getInstance().responseOperating(response, json);
        }else {
            String json = JsonUtil.failedJson(CommonValue.JSON_ROLE_ERROR);
            ServletUtil.getInstance().responseOperating(response, json);
        }
    }*/

    /**
     * @Description: 分页查询错误日志的接口
     * @Param: [username, pageNum, pageSize, fromTime, toTime, token, request, response]
     * @return: void
     * @Author: SongDaisong
     * @Date: 21:12
     */
    @CrossOrigin
    @RequestMapping(value = "/listErrorLogInfo",method = RequestMethod.GET)
    public void listErrorLogInfo(@RequestParam("username") String username,
                                 @RequestParam("pageNum") String pageNum,
                                 @RequestParam("pageSize") String pageSize,
                                 @RequestParam("fromTime") String fromTime,
                                 @RequestParam("toTime") String toTime,
                                 @RequestParam("token") String token,
                                 HttpServletRequest request, HttpServletResponse response)throws Exception{

        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myRole = session.getAttribute("role").toString();


        if(CommonValue.ROLE_AUDITOR.equals(myRole)){
            String json = logsInfoService.listLogInfo( username,pageNum, pageSize, fromTime, toTime,ErrorLogInfo.class,"errorLogInfo");
            ServletUtil.getInstance().responseOperating(response, json);
        }else {
            String json = JsonUtil.failedJson(CommonValue.JSON_ROLE_ERROR);
            LogUtil.produceWarningLogs(username,
                    CommonValue.WARNING_TYPE_1,
                    CommonValue.WARNING_LEVEL_0,
                    CommonValue.ERRLOGMODULE,
                    ServletUtil.getClientIp(request),
                    LogUtil.produceWarningMsg(CommonValue.WARNING_TYPE_1,username,RoleUtil.getRoleName(myRole),CommonValue.ERRLOGMODULE));
            ServletUtil.getInstance().responseOperating(response, json);
        }



    }

    /**
     * @Description: 分页查询告警日志的接口
     * @Param: [username, pageNum, pageSize, fromTime, toTime, token, request, response]
     * @return: void
     * @Author: SongDaisong
     * @Date: 21:12
     */
    @CrossOrigin
    @RequestMapping(value = "/listWarningLogInfo",method = RequestMethod.GET)
    public void listWarningLogInfo(@RequestParam("username") String username,
                                   @RequestParam("pageNum") String pageNum,
                                   @RequestParam("pageSize") String pageSize,
                                   @RequestParam("fromTime") String fromTime,
                                   @RequestParam("toTime") String toTime,
                                   @RequestParam("token") String token,
                                   HttpServletRequest request, HttpServletResponse response)throws Exception{

        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myRole = session.getAttribute("role").toString();

        if(CommonValue.ROLE_AUDITOR.equals(myRole)){
            String json = logsInfoService.listLogInfo(username, pageNum, pageSize, fromTime, toTime,WarningLogInfo.class,"warningLogInfo");
            ServletUtil.getInstance().responseOperating(response, json);
        }else {
            String json = JsonUtil.failedJson(CommonValue.JSON_ROLE_ERROR);
            LogUtil.produceWarningLogs(username,
                    CommonValue.WARNING_TYPE_1,
                    CommonValue.WARNING_LEVEL_0,
                    CommonValue.WARNINGLOGMODULE,
                    ServletUtil.getClientIp(request),
                    LogUtil.produceWarningMsg(CommonValue.WARNING_TYPE_1,username,RoleUtil.getRoleName(myRole),CommonValue.WARNINGLOGMODULE));
            ServletUtil.getInstance().responseOperating(response, json);
        }

    }

}
